package com.inibb.app.global;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;

import org.springframework.stereotype.Service;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.inibb.app.constant.GenericCodeConstant;
import com.inibb.app.service.admin.IGenericCodeService;
import com.inibb.app.service.admin.II18nMsgService;
import com.inibb.app.service.admin.ISaSystemParamService;
import com.inibb.app.vo.GenericCodeVO;
import com.inibb.app.vo.admin.SaSystemParamVO;
@Service("sysConfigBean")
public class SysConfigBean {
	
	//private Map<String, Object> sysConfigMap = new HashMap<String, Object>();
	
	public void load(ServletContext ctx){
		ctx.removeAttribute("i18nMessagesMap");
		ctx.removeAttribute("i18nGenericCodeMap");
		ctx.removeAttribute("codeTypes");
		ctx.removeAttribute("systemParamMap");
		ctx.removeAttribute("productTypes");
		
		IGenericCodeService genericCodeService = (IGenericCodeService) getBean(ctx,IGenericCodeService.class);
		ISaSystemParamService systemParamService = (ISaSystemParamService) getBean(ctx,ISaSystemParamService.class);
		II18nMsgService i18nMsgService = (II18nMsgService) getBean(ctx,II18nMsgService.class);
		//sysConfigMap = new HashMap<String, Object>();
		I18nMessagesMapBean i18nMessagesMap = new I18nMessagesMapBean();
		i18nMessagesMap.init(i18nMsgService);
		ctx.setAttribute("i18nMessagesMap", i18nMessagesMap);
		//i18n code map 
		I18nGenericCodeMapBean i18nGenericCodeMap = new I18nGenericCodeMapBean();
		i18nGenericCodeMap.init(genericCodeService);
		ctx.setAttribute("i18nGenericCodeMap", i18nGenericCodeMap);
		
		List<GenericCodeVO> codeTypes = genericCodeService.searchCodeType();
		ctx.setAttribute("codeTypes", codeTypes);
		
		//get system parameter
		List<SaSystemParamVO> systemParams = systemParamService.searchSystemParam();
		Map<String, SaSystemParamVO> systemParamMap = new HashMap<String, SaSystemParamVO>();
		for(SaSystemParamVO v:systemParams){
			systemParamMap.put(v.getName(), v);
		}
		ctx.setAttribute("systemParamMap", systemParamMap);
		
		//GET product type
		GenericCodeVO code = genericCodeService.searchCodeTypeByCode(GenericCodeConstant.CODE_TYPE_GOODS_TYPE);
		if(code!=null){
			List<GenericCodeVO> productTypes = genericCodeService.searchCodeByParentId(code.getCodeId());
			ctx.setAttribute("productTypes", productTypes);
		}
		//GET product unit
		GenericCodeVO codeUnit = genericCodeService.searchCodeTypeByCode(GenericCodeConstant.CODE_TYPE_PRODUCT_UNIT);
		if(codeUnit!=null){
			List<GenericCodeVO> productUnits = genericCodeService.searchCodeByParentId(codeUnit.getCodeId());
			ctx.setAttribute("productUnits", productUnits);
		}
	}
	
	/*public Map<String, Object> getSysConfigMap() {
		return sysConfigMap;
	}*/
	private Object getBean(ServletContext ctx,Class t){
		return WebApplicationContextUtils.getWebApplicationContext(ctx).getBean(t);
	}
}
